// 需要添加依赖：
// actix-web = "1.0.8"
// serde = { version = "1.0", features = ["derive"] }
use actix_web::{dev::Server, web, App, HttpResponse, HttpServer};

fn main() {
    let server = HttpServer::new(|| App::new().route("/", web::get.to(get_index)));

    println!("Serving on http://localhost:3000...");

    server
        .bind("127.0.0.1:3000")
        .expect("绑定地址时发送错误!")
        .run()
        .expect("服务器运行失败!");
}

fn get_index() -> HttpResponse {
    HttpResponse::Ok().content_type("text/html").body(
        r#"
            <title>GCD Calculator</title>
            <form action="/gcd" method="post">
            <input type="text" name="n"/>
            <input type="text" name="m"/>
            <button type="submit">Compute GCD</button>
            "#,
    )
}

fn gcd(mut n: u64, mut m: u64) -> u64 {
    assert!(n != 0 && m != 0);
    while m != 0 {
        if m < n {
            let t = m;
            m = n;
            n = t;
        }
        m = m % n;
    }
    n
}
